package com.ghh.app.photoframe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class PhotoFrameWidgetConfiguration extends Activity {
	private final static String			TAG					= "System.out";

	private int							appWidgetId			= AppWidgetManager.INVALID_APPWIDGET_ID;
	private int							IMG_REQUEST_CODE	= 1;

	private GridView					gridView;
	private List<Map<String, Object>>	iconList			= new ArrayList<Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setResult(RESULT_CANCELED);
		setContentView(R.layout.photo_frame_configuration);

		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}

		// If they gave us an intent without the widget id, just bail.
		if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}

		gridView = (GridView) findViewById(R.id.gridview);
		addIcon(R.drawable.add_icon);
		SimpleAdapter adapter = new SimpleAdapter(this, iconList, R.layout.item, new String[] { "icon" }, new int[] { R.id.icon });
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				int buttonIndex = iconList.size() - 1;
				if (position == buttonIndex) {
					openImageFile();
				}
			}
		});
	}

	public void save(View view) {
		Log.i(TAG, "save configuration");
		Intent resultValue = new Intent(this, PhotoFrameWidgetProvider.class);
		resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		resultValue.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
		setResult(RESULT_OK, resultValue);
		sendBroadcast(resultValue);
		finish();
	}

	public void cancel(View view) {
		finish();
	}

	public void openImageFile() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(intent, IMG_REQUEST_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == IMG_REQUEST_CODE) {
			Uri uri = data.getData();
			Log.i(TAG, "uri:" + uri.toString());

			PhotoFrame pf = new PhotoFrame(appWidgetId, getBaseContext());
			Set<String> images = pf.getImageSet();
			if (images == null) {
				images = new LinkedHashSet<String>();
				pf.setImageSet(images);
			}
			images.add(uri.toString());
			pf.save();

			updateGridView(uri);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void updateGridView(Uri uri) {
		addIcon(uri);
		((SimpleAdapter) gridView.getAdapter()).notifyDataSetChanged();
	}

	private void addIcon(Object res) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("icon", res);
		if (iconList.size() > 0) {
			iconList.add(iconList.size() - 1, map);
		} else {
			iconList.add(map);
		}
	}

	@Override
	public void onBackPressed() {
		Log.i(TAG, "back pressed");
		super.onBackPressed();
	}
}
